package com.example.utils;

import com.example.entity.AlarmAnalysis;
import com.example.entity.CapacityInformationAirspace;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class AlarmAnalysisUtil {
    public static AlarmAnalysis cacutate(List<CapacityInformationAirspace> input) {
        // 您的算法实现
        AlarmAnalysis result = new AlarmAnalysis();

        // 统计总告警数（不包括一级告警）
        int totalAlarms = (int) input.stream().filter(alarm -> alarm.getAlertLevel() != 1).count();
        result.setTotal(totalAlarms);

        // 统计各级告警数
        long level1Count = input.stream().filter(alarm -> alarm.getAlertLevel() == 1).count();
        long level2Count = input.stream().filter(alarm -> alarm.getAlertLevel() == 2).count();
        long level3Count = input.stream().filter(alarm -> alarm.getAlertLevel() == 3).count();
        long level4Count = input.stream().filter(alarm -> alarm.getAlertLevel() == 4).count();
        long level5Count = input.stream().filter(alarm -> alarm.getAlertLevel() == 5).count();

        result.setAlarm1Num((int) level1Count);
        result.setAlarm2Num((int) level2Count);
        result.setAlarm3Num((int) level3Count);
        result.setAlarm4Num((int) level4Count);
        result.setAlarm5Num((int) level5Count);

        // 统计每个空域编号的告警次数
        Map<String, Long> airspaceCountMap = input.stream()
                .filter(alarm -> alarm.getAlertLevel() != 1) // 排除一级告警
                .collect(Collectors.groupingBy(CapacityInformationAirspace::getAirspaceNumber, Collectors.counting()));

        // 找出告警次数最多的10个空域编号
        List<AlarmAnalysis.Pair> top10Airspaces = airspaceCountMap.entrySet().stream()
                .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
//                .limit(10)
                .map(entry -> {
                    AlarmAnalysis.Pair pair = new AlarmAnalysis.Pair();
                    pair.setAirspaceNumber(entry.getKey());
                    pair.setAlarmNum(entry.getValue().intValue());
                    return pair;
                })
                .collect(Collectors.toList());

        result.setPairList(top10Airspaces);

        return result;
    }
}
